import sys
sys.path.append("../../")

from model.StockBaseInfo import *
from model.CommonDb import *
from services.DayKlineService import *
import datetime
import pandas as pd
import ta

class AiController:
    def __init__(self,args):
        self.args = args
        self.service = StockBaseInfo()
        self.today = datetime.datetime.now().strftime("%Y-%m-%d")
        self.klineService = DayKlineService()
    def run(self):
        if self.args["func"] == "calcZhangtingRate":
            return self.calcZhangtingRate(self.args)

    """
    添加总体评论
    """
    def calcZhangtingRate(self,args):
        data = self.klineService.getDataByDate(args["code"], args["startDay"], args["endDay"])
        if data == "" or data == None or data == False or len(data) < 1:
            return False
        df = pd.DataFrame(data)

        # 计算简单移动平均线（SMA）
        df['sma_5'] = ta.trend.SMAIndicator(df['close'], window=5).sma_indicator()
        df['sma_10'] = ta.trend.SMAIndicator(df['close'], window=10).sma_indicator()

        # 计算相对强弱指数（RSI）
        df['rsi'] = ta.momentum.RSIIndicator(df['close'], window=14).rsi()

        print(df)

        # 简单的预测规则
        last_row = df.iloc[-1]
        if last_row['close'] > last_row['sma_5'] and last_row['close'] > last_row['sma_10'] and last_row['rsi'] < 70:
            return True
        else:
            return False


